package com.jh3502.vivo.function.me;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.view.SnackbarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * @author harisucici
 * @version 2018/11/26 11:49
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class MeActivity extends BaseActivity<MePresenter.MeView, MePresenter> implements MePresenter.MeView {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.include_user_detail_name)
    TextView includeUserDetailName;
    @BindView(R.id.include_user_detail_com)
    TextView includeUserDetailCom;
    @BindView(R.id.me_phone)
    EditText mePhone;
    @BindView(R.id.me_name)
    EditText meName;
    @BindView(R.id.me_sex)
    EditText meSex;
    @Override
    public MePresenter initPresenter() {
        return new MePresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_me;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("个人信息");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setMeView(this);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_done:
                Map<String, Object> body = new HashMap<>(2);
                body.put("mobile", mePhone.getText().toString().trim());
                body.put("name", meName.getText().toString().trim());
                body.put("sex", "1");
                mPresenter.toUpdateUser(body);
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        includeUserDetailName.setText(SpHelper.getInstance().getUserName());
        includeUserDetailCom.setText(SpHelper.getInstance().getDeptName());
        mePhone.setText(SpHelper.getInstance().getMobile());
        meName.setText(SpHelper.getInstance().getName());
        if ("0".equals(SpHelper.getInstance().getSex())) {
            meSex.setText("男");
        }


    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg, SnackbarUtil.ALERT);

    }


    @Override
    public void updateUserSuccess() {
        showShortToast("修改成功");
        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", SpHelper.getInstance().getUid());
        mPresenter.getUser(body);
    }

    @Override
    public void getUserSuccess() {
        mePhone.setText(SpHelper.getInstance().getMobile());
        meName.setText(SpHelper.getInstance().getName());
        if ("0".equals(SpHelper.getInstance().getSex())) {
            meSex.setText("男");
        }
    }
}
